package com.yanchengtect.demo.progress.rollback.activity.dao;

import com.yanchengtect.demo.progress.dao.BaseDao;
import com.yanchengtect.demo.progress.dao.RowMapperUtil;
import com.yanchengtect.demo.progress.rollback.activity.domain.Sorder;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 文心雕龙 Email：xuwenlong@shike8888.com
* @version 创建时间：2018年9月6日 下午6:21:06
* 类说明
*/
@Repository
public class SorderDao extends BaseDao<Sorder> {



	/**
	 * 根据活动id和时间查询订单列表
	 * @param act_id
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Sorder> listUnReciveByAidAndTime(Integer act_id,Date startTime,Date endTime){
		
		StringBuffer sql = new StringBuffer( "select order_id,user_id from sorder where status=4 and real_paymoney is null and type=1 and act_id = ? ");
		List<Object> args = new ArrayList<Object>();
		args.add(act_id);
		
		if (null != startTime) {
			sql.append(" and time > ? ");
			args.add(startTime);
		}

		if (null != endTime) {
			sql.append(" and time < ? ");
			args.add(endTime);
		}
		
		log.debug(sql.toString());

		return jdbcTemplate.query(sql.toString(), args.toArray(), new RowMapperUtil(Sorder.class));
	}

	
	/**
	 * 根据活动id和时间清除中奖未领取的订单
	 * @param actId 活动id
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public int DeleteUnRecives(int actId, Date startTime, Date endTime) {
		StringBuffer sql = new StringBuffer( "delete from sorder where status=4 and real_paymoney is null and type=1 and act_id = ? ");
		List<Object> args = new ArrayList<Object>();
		args.add(actId);
		
		if (null != startTime) {
			sql.append(" and time > ? ");
			args.add(startTime);
		}

		if (null != endTime) {
			sql.append(" and time < ? ");
			args.add(endTime);
		}
		
		log.debug(sql.toString());
		return jdbcTemplate.update(sql.toString(), args.toArray());
	}

 
}
